package spring.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import spring.mvc.model.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/json")
public class JSONController {
    @RequestMapping("/demo1")
    @ResponseBody
    //标识当前返回的是一个响应体
    public String getJSON() {
        return "hello,JSON";
    }

    @RequestMapping("demo2")
    @ResponseBody
    public List<String> getJSONArr() {
        List<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");

        return list;

    }


    @RequestMapping("demo3")
    @ResponseBody
    public Map<String,Integer> getJSONMap(){
        Map<String ,Integer> map = new HashMap<String,Integer>(){{
            put("key1",20);
            put("key2",30);
        }};
        return map;

    }

    @RequestMapping("demo4")
    @ResponseBody
    public User getJSONObj(){
        User user = new User();
        user.setUser_name("尼玛克");
        user.setAge(12);
        user.setSex("nan");
        user.setPassword("123");
        return user;
    }
}
